<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 9.2.&nbsp; Querying the Database with PHP Functions</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-9-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-9-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-9-SECT-2"></a>
<h3 id="631368-941" class="docSection1Title">9.2. Querying the Database with PHP Functions</h3>
<p class="docText">In this section, we introduce how to connect to a MySQL database with PHP. It's quite simple, and we'll begin shortly with examples, but we should talk briefly about what actually happens. When you try connecting to a MySQL database, the MySQL server authenticates you based on your username and password. PHP handles connecting to the database for you and allows you to immediately start performing queries and gathering data.</p>
<p class="docText">As in <a class="docLink" href="learnphpmysql-CHP-8.html#learnphpmysql-CHP-8">Chapter 8</a>, we'll need the same pieces of information to connect to the database:</p>
<ul><li><p class="docList">The IP address of the database server</p></li><li><p class="docList">The name of the database</p></li><li><p class="docList">The username</P></li><li><p class="docList">The password</P></LI></ul>
<p class="docText">If you're not sure what to use for these values, consult <a class="docLink" href="learnphpmysql-CHP-7.html#learnphpmysql-CHP-7">Chapter 7</a>. And, before moving on, make sure you can log into your database using the <tt>mysql</tt> command-line client.</P>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-9-FIG-1">Figure 9-1</a> shows how the steps of the database interaction relate to the two types of resources. Building the <tt>SELECT</tt> statement happens before the third function call but is not shown. It's done with plain PHP code, not a MySQL-specific PHP function.</p>
<a name="learnphpmysql-CHP-9-FIG-1"></a><p><center>
<h5 class="docFigureTitle">Figure 9-1. The interaction between functions and resources when using the database</H5>
<img border="0" alt="" id="195131084202" width="549" height="515" SRC="images/learnphpmysql_0901.jpg">
</center></p><br>
<a name="learnphpmysql-CHP-9-SECT-2.1"></a>
<H4 id="title-IDA15BXG" class="docSection2Title">9.2.1. Including Database Login Details</h4>
<p class="docText">You're going to create a file to hold the information for logging into MySQL. Storing this information in a file you include is recommended. If you change the database password, there is only one place that you need to change it regardless of how many PHP files you have that access the database.</p>
<P><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><TD valign="top">
<p class="docText">You don't have to worry about anyone directly viewing the file and getting your database login details.<a name="IDX-CHP-9-0420"></a> 
<a name="IDX-CHP-9-0421"></a> 
 The file, if requested by itself, is processed as a PHP file and returns a blank page.</p>
</td></tr></table></td></TR></table></p><BR>
<p class="docText">Let's call this file <span class="docEmphasis">db_login.php</span> and place it in the same directory as your other PHP files. The file is represented in <a class="docLink" href="#learnphpmysql-CHP-9-EX-1">Example 9-1</a>.</p>
<a name="learnphpmysql-CHP-9-EX-1"></a><H5 id="title-IDABBCXG" class="docExampleTitle">Example 9-1. PHP file format</H5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$db_host='<tt><i>hostname of database server</i></tt>';
$db_database='<tt><i>database name</i></tt>';
$db_username='<tt><i>username</i></tt>';
$db_password='<tt><i>password</i></tt>';
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-9-EX-2">Example 9-2</a>, we created this file to use a database on the same machine as the web server. We assign it a database name, username, and password.</P>
<a name="learnphpmysql-CHP-9-EX-2"></a><h5 id="title-IDA1BCXG" class="docExampleTitle">Example 9-2. The db_login.php file with values filled in</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
$db_host='localhost';
$db_database='test';
$db_username='test';
$db_password='yourpass';
?&gt;
</pre><BR>

</td></tr></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-9-FIG-2">Figure 9-2</a> illustrates how you're going to use this file with other PHP files. You're going to continue using the database that you started to set up in <a class="docLink" href="learnphpmysql-CHP-7.html#learnphpmysql-CHP-7">Chapter 7</a>.</P>
<a name="learnphpmysql-CHP-9-FIG-2"></a><p><center>
<h5 class="docFigureTitle">Figure 9-2. Reusing the login details in multiple files</H5>
<img border="0" alt="" id="195131084202" width="549" height="334" SRC="images/learnphpmysql_0902.jpg">
</center></p><br>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-9-EX-3">Example 9-3</a> is an abbreviated dump of the database created from the <tt>mysqldump</tt> command.</P>
<a name="learnphpmysql-CHP-9-EX-3"></a><h5 id="title-IDAGDCXG" class="docExampleTitle">Example 9-3. The SQL to recreate the test objects</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
--
-- Table structure for table `authors`
--
DROP TABLE IF EXISTS `authors`;
CREATE TABLE `authors` (
  `author_id` int(11) NOT NULL auto_increment,
  `title_id` int(11) NOT NULL default '0',
  `author` varchar(125) default NULL,
  PRIMARY KEY  (`author_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `authors`
--
INSERT INTO `authors` VALUES (1,1,'Ellen Siever'),(2,1,'Aaron Weber'),(3,2,
'Arnold Robbins'),(4,2,'Nelson H.F. Beebe');
--
-- Table structure for table `books`
--
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
  `title_id` int(11) NOT NULL auto_increment,
  `title` varchar(150) default NULL,
  `pages` int(11) default NULL,
  PRIMARY KEY  (`title_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `books`
--
INSERT INTO `books` VALUES (1,'Linux in a Nutshell',476),(2,'Classic Shell
Scripting',256);
--
-- Table structure for table `purchases`
--
DROP TABLE IF EXISTS `purchases`;
CREATE TABLE `purchases` (
  `id` int(11) NOT NULL auto_increment,
  `user` varchar(10) default NULL,
  `title` varchar(150) default NULL,
  `day` date default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `purchases`
--
LOCK TABLES `purchases` WRITE;
INSERT INTO `purchases` VALUES (1,'Mdavis','Regular Expression Pocket Reference',
'2005-02-15'),(2,'Mdavis','JavaScript &amp; DHTML Cookbook','2005-02-10');
</pre><BR>

</td></tr></table></p>
<p class="docText">If you didn't create the tables in the last chapter, the code in <a class="docLink" href="#learnphpmysql-CHP-9-EX-3">Example 9-3</a> can be saved as <span class="docEmphasis">backup.sql</span> and run from the command prompt with the following:</p>
<pre>
mysql -u <tt><I>username</i></tt> -p <tt><I>password</i></tt> -D <tt><I>database_name</I></tt> &lt; <tt><I>backupfile.sql</i></tt>
</pre><br>

<p class="docText">The database is called <tt>test</tt>, and it consists of three tables called <tt>books, authors</tt>, and <tt>purchases</tt>. Each table has a few sample rows. That's enough to get us started querying from PHP.</p>

<a name="learnphpmysql-CHP-9-SECT-2.2"></a>
<h4 id="title-IDAOECXG" class="docSection2Title">9.2.2. Connecting to the Database</h4>
<a name="IDX-CHP-9-0422"></a> 
<a name="IDX-CHP-9-0423"></a> 

<p class="docText">The first thing you need to do is connect to the database and check to make sure there's a connection. Including the file that you set up to store your connection information allows you to use the variables instead of hardcoded values when you call the <tt>mysql_connect</tt> function, as shown in <a class="docLink" href="#learnphpmysql-CHP-9-EX-4">Example 9-4</a>.</p>
<a name="learnphpmysql-CHP-9-EX-4"></a><h5 id="title-IDAIFCXG" class="docExampleTitle">Example 9-4. Including the connection values and calling mysql_connect</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
include('db_login.php');
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die ("Could not connect to the database: &lt;br /&gt;". mysql_error());
}
?&gt;
</pre><br>

</td></tr></table></P>
<p class="docText">The <tt>mysql_connect</tt> function takes the database host, username, and password as parameters. If the connection is successful, a link to a database is returned. <tt>FALSE</tt> is returned if a connection can't be made. Check the return value from the function to make sure there's a connection. If there's a problem, such as an incorrect password, print out a polite warning and the reason for the error using <tt>mysql_error</tt>.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><TR><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><TD width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><td valign="top">
<p class="docText">Instead of simply echoing an error message, use the <tt>die</tt> function to display the error, and then stop the program, Not being able to access the database makes most database-driven pages fairly useless and prevents the user from seeing numerous errors.</p>
</TD></tr></table></td></TR></table></p><br>
<p class="docText">Notice that we didn't specify the database name yet.</P>
<a name="learnphpmysql-CHP-9-SECT-2.2.1"></a>
<h5 id="title-IDAGGCXG" class="docSection3Title">9.2.2.1. Troubleshooting connection errors</h5>
<a name="IDX-CHP-9-0424"></a> 
<a name="IDX-CHP-9-0425"></a> 
<a name="IDX-CHP-9-0426"></a> 

<p class="docText">One error you may get is:</p>
<pre>
Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache
Group\Apache2\htdocs\test.php on line 4
</pre><br>

<p class="docText">This occurs because PHP 5.1.2 for Windows was downloaded, and MySQL support was not included by default. To fix this error, copy the <span class="docEmphasis">php_mysql.dll</span> file from the <span class="docEmphasis">ext/</span> directory of the PHP zip file to <span class="docEmphasis">C:\php</span>, and then edit lines 461 and 589 of <span class="docEmphasis">C:\WINDOWS\php.ini</span>. This will change the extension to include the directory to <span class="docEmphasis">C:/php</span> and uncommenting the MySQL extension line, respectively.</P>
<p class="docText">You'll need to restart Apache, and then MySQL support will be enabled.</P>


<a name="learnphpmysql-CHP-9-SECT-2.3"></a>
<h4 id="title-IDAPHCXG" class="docSection2Title">9.2.3. Selecting the Database</h4>
<p class="docText">Now that you're connected, the next step is to select which database to use with the <tt>mysql_select_db</tt> command. It takes two parameters: the database name and, optionally, the database connection. If you don't specify the database connection, the default is the connection from the last <tt>mysql_connect</tt>.</p>
<pre>
$db_select = mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: &lt;br /&gt;". mysql_error());
}
</pre><br>

<p class="docText">Again, it's good practice to check for an error and display it every time you access the database.</P>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><TR><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><TD width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><td valign="top">
<p class="docText">While it's possible to call <tt>mysql_select_db</tt> multiple times within the same script, it's not considered good practice. Generally, you should be able to do all of your work with one database. Maintaining connections and results to multiple databases are beyond this book's scope.</p>
</td></tr></table></td></tr></table></p><br>
<p class="docText">Now that you've got a good database connection, you're ready to execute your SQL query.</p>

<a name="learnphpmysql-CHP-9-SECT-2.4"></a>
<h4 id="title-IDAQICXG" class="docSection2Title">9.2.4. Building the SQL SELECT Query</h4>
<a name="IDX-CHP-9-0427"></a> 
<a name="IDX-CHP-9-0428"></a> 
<a name="IDX-CHP-9-0429"></a> 
<a name="IDX-CHP-9-0430"></a> 
<a name="IDX-CHP-9-0431"></a> 

<p class="docText">Building a SQL query is as easy as setting a variable to the string that is your SQL query. Of course, you'll need to use a valid SQL query, or MySQL returns with an error when you execute the query. The variable name <tt>$query</tt> is used, but you can choose anything you'd like for a variable name. The SQL query in this example is <tt>SELECT * FROM books</tt>.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><TD valign="top">
<p class="docText">Unlike when you used the <tt>mysql</tt> command-line client, the query does not have a semicolon at the end.</p>
</TD></tr></table></td></tr></table></P><br>
<p class="docText">You can build up your query in parts using the string concatenate (<tt>.</tt>) operator:</p>
<pre>
$select = ' SELECT ';
$column = ' * ';
$from = ' FROM ';
$tables = ' `books` ';
$where = '';
$query = $select.$column.$from.$tables.$where;
</pre><BR>

<p class="docText">Which is a more flexible version of this:</p>
<pre>
$query = "SELECT * FROM books";
</pre><br>

<p class="docText">The query string could also use a variable in the <tt>WHERE</tt> clause to limit which rows are returned based on user information or another query.</P>
<p class="docText">Now that you have your query assigned to a variable, you can execute it.</p>

<a name="learnphpmysql-CHP-9-SECT-2.5"></a>
<h4 id="title-IDATKCXG" class="docSection2Title">9.2.5. Executing the Query</h4>
<p class="docText">To have the database execute the query, use the <tt>mysql_query</tt> function. It takes two parametersthe query and optionally the database linkand returns the result. Save a link to the results in a variable called, you guessed it, <tt>$result</tt>! This is also a good place to check the return code from <tt>mysql_query</tt> to make sure that there were no errors in the query string or the database connection by verifying that <tt>$result</tt> is not <tt>FALSE</tt>.</p>
<pre>
$result = mysql_query( $query );
if (!$result)
{
die ("Could not query the database: &lt;br /&gt;". mysql_error());
}
</pre><BR>

<p class="docText">When the database executes the query, all of the results form a result set. These correspond to the rows that you saw upon doing a query using the <tt>mysql</tt> command-line client. To display them, you process each row, one at a time.</P>

<a name="learnphpmysql-CHP-9-SECT-2.6"></a>
<h4 id="title-IDAOLCXG" class="docSection2Title">9.2.6. Fetching and Displaying</h4>
<a name="IDX-CHP-9-0432"></a> 
<a name="IDX-CHP-9-0433"></a> 
<a name="IDX-CHP-9-0434"></a> 
<a name="IDX-CHP-9-0435"></a> 

<p class="docText">Use <tt>mysql_fetch_row</tt> to get the rows from the result set. It takes the result you stored in <tt>$result</tt> from the query as a parameter. It returns one row at a time from the query until there are no more rows, and then it returns <tt>FALSE</tt>. Therefore, you do a loop on the result of <tt>mysql_fetch_row</tt> and define some code to display each row:</p>
<pre>
while ($result_row = mysql_fetch_row($result)){
       echo $result_row[2] . '&lt;br /&gt;';
}
</pre><br>

<a name="learnphpmysql-CHP-9-SECT-2.6.1"></a>
<H5 id="title-IDAXMCXG" class="docSection3Title">9.2.6.1. Fetch types</h5>
<p class="docText">This is not the only way to fetch the results. Using <tt>mysql_fetch_array</tt>, PHP can place the results into an array in one step. It takes a result as its first parameter, and the way to bind the results as an optional second parameter. If <tt>MYSQL_ASSOC</tt> is specified, the results are indexed in an array based on their column names in the query. If <tt>MYSQL_NUM</tt> is specified, then the number starting at zero accesses the results. The default value, <tt>MYSQL_BOTH</tt>, returns a result array with both types. The <tt>mysql_fetch_assoc</tt> is an alternative to supplying the <tt>MYSQL_ASSOC</tt> argument.</P>
<p class="docText">If you rewrote the code above to use <tt>mysql_fetch_array</tt> with an associative indexed array, it would look like this:</p>
<pre>
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                                echo $row[title]. '&lt;br /&gt;';
}
</pre><BR>



<a name="learnphpmysql-CHP-9-SECT-2.7"></a>
<H4 id="title-IDAVNCXG" class="docSection2Title">9.2.7. Closing the Connection</H4>
<p class="docText">As a rule of thumb, you always want to close a connection to a database when you're done using it. Closing a database with <tt>mysql_close</tt> will tell PHP and MySQL that you no longer will be using the connection, and will free any resources and memory allocated to it.</p>
<pre>
mysql_close($connection)
</pre><br>


<a name="learnphpmysql-CHP-9-SECT-2.8"></a>
<h4 id="title-IDADOCXG" class="docSection2Title">9.2.8. Putting It All Together</h4>
<p class="docText">Now you're going to take all of the steps and put them into a single PHP file that you'll call <span class="docEmphasis">db_test.php</span>. You should place the PHP script shown in <a class="docLink" href="#learnphpmysql-CHP-9-EX-5">Example 9-5</a> in the same directory as the <span class="docEmphasis">db_login.php</span> file.</p>
<a name="learnphpmysql-CHP-9-EX-5"></a><h5 id="title-IDAVOCXG" class="docExampleTitle">Example 9-5. Displaying the books and authors</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
// Include our login information
include('db_login.php');
// Connect
$connection = mysql_connect( $db_host, $db_username, $db_password );
if (!$connection)
{
   die ("Could not connect to the database: &lt;br /&gt;". mysql_error());
}
// Select the database
$db_select=mysql_select_db($db_database);
if (!$db_select)
{
   die ("Could not select the database: &lt;br /&gt;". mysql_error());
}
// Assign the query
$query = "SELECT * FROM `books` NATURAL JOIN `authors`";
// Execute the query
$result = mysql_query( $query );
if (!$result)
{
   die ("Could not query the database: &lt;br /&gt;". mysql_error());
}

// Fetch and display the results
while ($result_row = mysql_fetch_row(($result)))
{
       echo 'Title: '.$result_row[1] . '&lt;br /&gt;';
       echo 'Author: '.$result_row[4] . '&lt;br /&gt; ';
       echo 'Pages: '.$result_row[2] . '&lt;br /&gt;&lt;br /&gt;';
}
/ /Close the connection
mysql_close($connection);
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">Here's the output from <a class="docLink" href="#learnphpmysql-CHP-9-EX-5">Example 9-5</a>:</P>
<pre>
Title: Linux in a Nutshell&lt;br /&gt;Author: Ellen Siever&lt;br /&gt; Pages: 476&lt;br /&gt;
&lt;br /&gt;Title: Linux in a Nutshell&lt;br /&gt;Author: Aaron Weber&lt;br /&gt; Pages: 476&lt;br /&gt;
&lt;br /&gt;Title: Classic Shell Scripting&lt;br&gt;Author: Arnold Robbins&lt;br /&gt; Pages: 256&lt;br /&gt;
&lt;br /&gt;Title: Classic Shell Scripting&lt;br /&gt;Author: Nelson H.F. Beebe&lt;br /&gt; Pages:
256&lt;br /&gt;&lt;br /&gt;
</pre><br>

<p class="docText">This displays in your browser as in <a class="docLink" href="#learnphpmysql-CHP-9-FIG-3">Figure 9-3</a>.</p>
<a name="learnphpmysql-CHP-9-FIG-3"></a><P><center>
<H5 class="docFigureTitle">Figure 9-3. How <a class="docLink" href="#learnphpmysql-CHP-9-EX-5">Example 9-5</a> displays in the browser</h5>
<img border="0" alt="" id="195131084202" width="530" height="394" SRC="images/learnphpmysql_0903.jpg">
</center></P><br>
<p class="docText">If you don't see the screen in <a class="docLink" href="#learnphpmysql-CHP-9-FIG-3">Figure 9-3</a>, then you'll see an error from whichever step in the process had a problem, giving you an idea of what went wrong and where it was wrong.</p>
<p class="docText">To make the display more appealing, you can put the information into a table, as shown in <a class="docLink" href="#learnphpmysql-CHP-9-EX-6">Example 9-6</a>. You also add complete HTML headers.</p>
<a name="learnphpmysql-CHP-9-EX-6"></a><H5 id="title-IDAVQCXG" class="docExampleTitle">Example 9-6. Displaying the results of a query in an HTML table</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Displaying in an HTML table&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;th&gt;Title&lt;/th&gt;
&lt;th&gt;Author&lt;/th&gt;
&lt;th&gt;Pages&lt;/th&gt;
&lt;/tr&gt;
&lt;?php
//Include our login information
include('db_login.php');
// Connect
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die("Could not connect to the database: &lt;br /&gt;". mysql_error());
}
// Select the database
$db_select = mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: &lt;br /&gt;". mysql_error());
}
// Assign the query
$query = "SELECT * FROM `books` NATURAL JOIN `authors`";
// Execute the query
$result = mysql_query($query);
if (!$result){
die ("Could not query the database: &lt;br /&gt;". mysql_error());
}
// Fetch and display the results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$title = $row["title"];
$author = $row["author"];
$pages = $row["pages"];
echo "&lt;tr&gt;";
echo "&lt;td&gt;$title&lt;/td&gt;";
echo "&lt;td&gt;$author&lt;/td&gt;";
echo "&lt;td&gt;$pages&lt;/td&gt;";
echo "&lt;/tr&gt;";
}
// Close the connection
mysql_close($connection);
?&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><br>

</TD></tr></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-9-EX-6">Example 9-6</a> displays in your browser as shown in <a class="docLink" href="#learnphpmysql-CHP-9-FIG-4">Figure 9-4</a>.</p>
<a name="learnphpmysql-CHP-9-FIG-4"></a><p><center>
<H5 class="docFigureTitle">Figure 9-4. The same data but in an HTML table</H5>
<img border="0" alt="" id="195131084202" width="525" height="232" SRC="images/learnphpmysql_0904.jpg">
</center></p><br>
<p class="docText">Notice that you made use of the <tt>MYSQL_ASSOC</tt> fetch type in <a class="docLink" href="#learnphpmysql-CHP-9-EX-6">Example 9-6</a>. You're probably saying to yourself, "That's great, but how do I display the book titles with the authors all on one line?" This is where we talk about PEAR.</p>


</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-9-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-9-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
